package com.gin.kotlin.no_gay_alliance.adapter.post

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.adapter.post.UploadedAttachmentAdapter.Companion.configAttachmentImageView
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.ContentFile
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemUploadBodyBinding
import com.gin.kotlin.no_gay_alliance.enums.HandlerMessageWhat
import com.gin.kotlin.no_gay_alliance.enums.RetweetType
import com.gin.nga.response.post.UploadBody

class UploadBodyAdapter(context: Context, handler: MyHandler, data: MutableList<UploadBody> = mutableListOf()) :
    AbstractBindingRecyclerViewAdapter<ItemUploadBodyBinding, UploadBody>(context, handler, false, data), View.OnClickListener {

    lateinit var contentFiles: MutableList<ContentFile>
    lateinit var launcher: ActivityResultLauncher<Intent>

    override fun onBindViewHolder(holder: ViewHolder<ItemUploadBodyBinding>, position: Int, b: ItemUploadBodyBinding, dataModel: UploadBody) {
        val iv = b.ivUploadBody
        val tv = b.tvUploadBody
        listOf(iv, tv).forEach { it.setOnClickListener(this) }
        if (position == 0) {
            // 上传按钮
            iv.setImageResource(R.drawable.ic_upload)
            iv.setBackgroundResource(R.drawable.shape_upload_background)
            tv.text = context.getString(R.string.tv_upload_body_delete)
            listOf(iv, tv).forEach { it.tag = null }
        } else {
            // 已上传附件按钮
            iv.background = null
            tv.setText(R.string.tv_upload_body)

            // 根据文件后缀，选择图标显示的内容
            val contentFile = contentFiles[position - 1]

            configAttachmentImageView(iv,contentFile.ext?.lowercase()){ it.setImageURI(contentFile.uri) }

            listOf(iv, tv).forEach { it.tag = dataModel }
        }
    }

    /**
     * 同步body列表到activity
     */
    fun sync() {
        handler.sendMessage(HandlerMessageWhat.SYNC_UPLOAD_BODY, data)
    }

    override fun onClick(v: View?) {
        val uploadBody = v?.tag as? UploadBody
        if (uploadBody == null) {
            // 请求选择文件
            launcher.launch(Intent().also {
                it.action = Intent.ACTION_GET_CONTENT
                it.type = "*/*"
            })
        } else {
            when (v.id) {
                R.id.iv_upload_body -> {
                    // 获取来源标注信息，放入error字段一起传递
                    val contentFile = contentFiles[data.indexOf(uploadBody) - 1]
                    uploadBody.error = RetweetType.find(contentFile.originalName ?: "")
                    handler.sendMessage(HandlerMessageWhat.ON_CLICK_ATTACHMENT, uploadBody)
                }

                R.id.tv_upload_body -> {
                    // 删除附件
                    AlertDialogUtils.builder(context, context.getString(R.string.dialog_title_delete_attachment), listener = { _, which ->
                        if (which == DialogInterface.BUTTON_POSITIVE) {
                            removeData(data.indexOf(uploadBody))
                            handler.sendMessage(HandlerMessageWhat.ON_DELETED_ATTACHMENT, uploadBody)
                        }
                    }).show()
                }
            }
        }
    }
}